<?php

class Antz_Form_Components_Input extends Antz_Form_Component
{

    public function __construct($type, $name, $label, $value='', $afterText=''){
        $id = 'input_'.$type.rand();
        $this->setId($id);
        $this->type = $type;
        $this->name = $name;
        $this->label = $label;
        $this->value = $value;
        $this->addAftertext($afterText);
        if($type=='text' || $type=='password') $this->maxlength = 255;
        if($type == 'file' && is_object($this->formObj)) $this->formObj->setEncType('multipart/form-data');
    }

    public function render(){
    	$Antz = Antz::registry('Antz');
    	global $gCms;
        if($this->isXhtml()) $closeTag = ' />';
        else $closeTag = '>';
        $disabled = ($this->disabled) ? 'disabled' : '';
        if(Antz::isFormError($this->name)){
          $this->addClass('error');
          //$this->aftertext = Antz::getFormError($this->name).$this->aftertext;
        }
        if($this->required) $this->aftertext = '<span class="required"><img src="'.$gCms->config['root_url'].'/modules/Antz/images/required.gif" alt="'.$Antz->Lang('required').'" /></span>'.$this->aftertext;
        switch($this->type){
            case 'hidden':
                $returnString = '<input type="hidden" name="'.$this->name.'" id="'.$this->getId().'" value="'.$this->value.'">';
            break;
            case 'text':
                $returnString = '<input type="text" name="'.$this->name.'" id="'.$this->getId().'" value="'.$this->value.'" maxlength="'.$this->maxlength.'"'.$this->renderParameters().$this->renderClasses().$disabled.$closeTag;
                if($this->showLabel) $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
            break;
            case 'password':
                $returnString = '<input type="password" name="'.$this->name.'" id="'.$this->getId().'" value="'.$this->value.'" maxlength="'.$this->maxlength.'"'.$this->renderParameters().$this->renderClasses().$disabled.$closeTag;
                if($this->showLabel) $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
            break;
            case 'submit':
                $returnString = '<input type="submit" name="'.$this->name.'" value="'.$this->value.'" id="'.$this->getId().'"'.$this->renderParameters().$this->renderClasses().$disabled.$closeTag;
                if($this->showLabel) $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
            break;
            case 'button':
                $returnString = '<input type="button" name="'.$this->name.'" value="'.$this->value.'" id="'.$this->getId().'"'.$this->renderParameters().$this->renderClasses().$disabled.$closeTag;
                if($this->showLabel) $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
            break;
            case 'textarea':
                $returnString = '<textarea name="'.$this->name.'" id="'.$this->getId().'"'.$this->renderParameters().$this->renderClasses().$disabled.'>'.$this->value.'</textarea>';
                if($this->showLabel) $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
            break;
            case 'raw':
                $returnString = '<div class="raw"'.$this->renderClasses().$disabled.'>'.$this->value.'</div>';
                if($this->showLabel) $returnString = '<label>'.$this->label.'</label>'.$returnString;
            break;
            case 'file':
                $returnString = '<input type="file" name="'.$this->name.'" id="'.$this->getId().'" '.$this->renderParameters().$this->renderClasses().$disabled.$closeTag;
                if($this->showLabel) $returnString = '<label for="'.$this->getId().'">'.$this->label.'</label>'.$returnString;
            break;
        };
        $returnString .= '<div class="aftertext">'.$this->aftertext.'</div>';
        if($this->wrap) $returnString = $this->wrap($returnString);
        return $returnString;
    }
}
